package com.atguigu.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * Created by 黄凯 on 2023/6/30 0030 18:58
 *
 * @author 黄凯
 * 永远相信美好的事情总会发生.
 * <p>
 * 单例Connection
 */
public class Hbase02_SingletonConnection {

    private static Connection connection = null;

    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        // 创建同步的Connection对象
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //获取connection的方法
    public static Connection getConnection() {
        return connection;
    }

    //关闭connection的方法
    public static void closeConnection() {
        if (connection != null && !connection.isClosed()) {
            try {
                connection.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
